package fast.main.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import fast.main.service.BaseService;
import fast.main.util.Super;
/**
 * 首页控制
 * @author tenviy
 * 
 */

@Controller
public class IndexController extends Super{

	@Autowired
	BaseService bs;

	/*@RequestMapping("index.do")
	public String init(HttpServletRequest request,HttpServletResponse response){
		List<Map<String, Object>> list = bs.query("select * from xtgl_gncd where sj_gnxh=0 order by px");
		
		return "index";
	}*/
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("index.do")
	public String init(HttpServletRequest request,HttpServletResponse response,@RequestParam Map<String, String> form){
		Object o=new Object();
		try {
			//获取主文本，格式ctrl=ipx_getWB;
			// 	ipx为类名，getWB为方法名;
			//       如ctrl=ipx时，默认方法名为init;
			String ctrl=form.get("ctrl");
			String cla=ctrl;
			String met="init";
			String arrs[]=ctrl.split("_");
			if(arrs.length==2){
				cla=arrs[0];
				met=arrs[1];
			}
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("request", request);
			map.put("response", response);
			map.put("bs", bs);
			map.put("form", form);
			Class cc=Class.forName("fast.app."+cla);
			Method method=cc.getMethod(met,Map.class);
			o=method.invoke(cc.newInstance(), map);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return o.toString();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("ajax.do")
	@ResponseBody
	public void init1(HttpServletRequest request,HttpServletResponse response,@RequestParam Map<String, String> form){
		Object o=new Object();
		try {
			//获取主文本，格式ctrl=ipx_getWB;
			// 	ipx为类名，getWB为方法名;
			//       如ctrl=ipx时，默认方法名为init;
			String ctrl=form.get("ctrl");
			String cla=ctrl;
			String met="init";
			String arrs[]=ctrl.split("_");
			if(arrs.length==2){
				cla=arrs[0];
				met=arrs[1];
			}
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("request", request);
			map.put("response", response);
			map.put("bs", bs);
			map.put("form", form);
			Class cc=Class.forName("fast.app."+cla);
			Method method=cc.getMethod(met,Map.class);
			o=method.invoke(cc.newInstance(), map);
			response.setContentType("application/json");
	        response.setHeader("Pragma", "No-cache");
	        response.setHeader("Cache-Control", "no-cache");
	        response.setCharacterEncoding("UTF-8");
	        PrintWriter out= null;
	        out = response.getWriter();
	        out.print(o.toString());
	        out.flush();
	        out.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
